var STATE = {"回收站": "recycleBin", "待办": "Agency", "进行": "conduct", "完成": "complete", "发布": "release"};
$(document).ready(function () {
    $("#weChatSetUp span").click();
});
function getWeChat(OneElement) {
    $(".activeSpan").removeClass("activeSpan");
    $(OneElement).addClass("activeSpan");
    $("#weChat").show();
    $("#preservationWeChat").show();
    $("#setting").hide();
    $("#preservationSetting").hide();
    getSettingData();
}
function getSetting(OneElement) {
    $(".activeSpan").removeClass("activeSpan");
    $(OneElement).addClass("activeSpan");
    $("#weChat").hide();
    $("#preservationWeChat").hide();
    $("#setting").show();
    $("#preservationSetting").show();
    doGetAllState();
    doGetWorkState();
}
function doGetAllState() {
    var workStateDiv = $("#workStateDiv");
    workStateDiv.empty();
    $.post("doGetAllState", function (result) {
        if (result.state == 1) {
            var list = result.data;
            for (var i in list) {
                workStateDiv.append(
                    '<div class="col-sm-2">' +
                    '<div>' +
                    '<span class="workStateTitle">' + list[i].stateName + '</span>' +
                    '<span class="deleteBtn" onclick="toDeleteState(\'' + list[i].stateID + '\')">删除</span>' +
                    '</div>' +
                    '<div class="input-group colorpicker-component" title="Using format option" id="' + list[i].stateID + '">' +
                    '<span class="controlColor">颜色</span>' +
                    '<input id="' + STATE[list[i].stateName] + '" type="text" class="form-control input-lg" value="' + list[i].stateColor + '"/>' +
                    '<span class="input-group-addon"><i style="width: 32px;height:20px;background-color:' + list[i].stateColor + '"></i></span>' +
                    '</div>' +
                    '</div>')
            }
            workStateDiv.append(
                '<div class="col-sm-2" id="addStateModel" hidden="hidden">' +
                '<div>' +
                '<input name="stateName" id="stateName" value="" placeholder="请输入"/>' +
                '<button id="addStateBtn" type="button" class="btn btn-primary" onclick="addState()">确认</button>' +
                '<button id="hideAddStateBtn" type="button" class="btn btn-primary" onclick="hideAddState()">取消</button>' +
                '</div>' +
                '<div class="input-group colorpicker-component" title="Using format option">' +
                '<span class="controlColor">颜色</span>' +
                '<img id="controlImg" src="../PM/images/color.png"/>' +
                '<input id="newStateColor" type="text" class="form-control input-lg"/>' +
                '<span class="input-group-addon"><i style="width: 32px;height:20px;opacity: 0;" onclick="clearOpacity(this)"></i></span>' +
                '</div>' +
                '</div>' +
                '<div class="col-sm-2 partWhite" id="stateModule" onclick="showState()">添加状态</div>');
        } else {
            alert("获取状态数据失败");
        }
        $('.input-group').colorpicker({format: "rgba"});
    })
}
function showState() {
    $("#addStateModel").show();
}
function hideAddState(){
    $("#addStateModel").hide();
}
function addState() {
    var params={
        "StateName":$("#stateName").val(),
        StateColor:$("#newStateColor").val()
    };
    $.post("doCreateState",params,function (result) {
        if(result.state==1){
            showMessage("添加成功");
            $("#settingSetUp>span").click();
            $("#addStateModel").hide();
        }else{
            showMessage("添加失败");
        }
    })
}
function toDeleteState(ID) {
    $.post("doDeleteState",{"StateID":ID},function (result) {
        if(result.state==1){
            showMessage("删除成功");
            $("#settingSetUp>span").click();
        }else{
            showMessage("删除失败");
        }
    })
}
function clearOpacity(Element){
    $(Element).css("opacity","1");
    $("#controlImg").hide();
}
function preservationSetting() {
    var options = $("#urgentRemind option");
    var urgentRemindDay='';
    for (var i = 0; i < options.length; i++) {
        if (options[i].selected == true) {
            urgentRemindDay=options[i].value;
        }
    }
    $.post("doUpdateWorkState",{"WorkStateValue":urgentRemindDay},function (result) {
        if(result.state==1){
            showMessage("保存成功");
            $("#settingSetUp>span").click();
        }else{
            showMessage("保存失败");
        }
    })
    var params = '';
    var element=$(".colorpicker-element");
    for (var i = 0; i < element.length; i++) {
        if(element[i].id){
            if(params.length == 0){
                params=element[i].id+":"+$("#"+element[i].id+">input").val();
            }else{
                params+=";"+element[i].id+":"+$("#"+element[i].id+">input").val();
            }
        }
    }
    $.post("doUpdateState",{"jsonStr":params},function (result) {
        if(result.state==1){
            showMessage("保存成功");
            $("#settingSetUp>span").click();
        }else{
            showMessage("保存失败");
        }
    })

}
function doGetWorkState() {
    $.post("doGetWorkState", function (result) {
        if (result.state == 1) {
            var list = result.data;
            var options = $("#urgentRemind option");
            for (var i = 0; i < options.length; i++) {
                if (options[i].value == list[0].workStateValue) {
                    $(options[i]).attr("selected", "selected")
                }
            }
        } else {
            showMessage("获取紧急提醒时间失败");
        }
    })
}
function preservationWeChat(){
   var access = $("input[name='workType'][checked]").val();
   var PushType = $("#pushType>option:selected").html();
   var PushTime = $("#pushTime").val();
   var weChatModuleID = $("#dataTemplate").val();
   var pushUrl = $("#callURL").val();
   var forwardUrl = $("#detailsUrL").val();
   var statisticsTime = $("#StatisticsTime").val();
   var params ={
      "access":access,
      "PushType":PushType,
      "PushTime":PushTime,
      "weChatModuleID":weChatModuleID,
      "pushUrl":pushUrl,
      "forwardUrl": forwardUrl,
      "StatisticsTime": statisticsTime
   };
   $.post("updateWeChatMessage",params,function(result){
         if(result.state == 1){
         showMessage("保存成功");
         }else{
         showMessage("保存失败");
         }
     })
}
function getSettingData(){
   $.post("doGetSettingData",function(result){
         if(result.state == 1){
            var list = result.data;
             $("input[name='workType'][value='"+list.access+"']").attr("checked",'checked');
             $("#pushTime>option[value='"+list.pushType+"']").attr("selected","selected");
             $("#pushTime").val(list.pushTime);
             $("#dataTemplate").val(list.weChatModuleID);
             $("#callURL").val(list.pushUrl);
             $("#detailsUrL").val(list.forwardUrl);
             $("#StatisticsTime").val(list.statisticsTime);
         }else{
            showMessage("数据获取失败");
         }
     })

}
function updateState(Check,cancelCheck){
   $("#"+Check).attr("checked", "checked");
   $("#"+cancelCheck).attr("checked",false);
}
